<?php defined('SYSPATH') or die('No direct script access.');

	class Controller_Login extends Controller{
		
		public function action_index(){
			
			$loginView = View::factory("login");
			$loginView->title = __('Systole - 1.0 | Iniciar Sesión');
			$loginView->header = "";//View::factory("template-sections/login-header")->render();
			$loginView->content = View::factory("auth/logintabs")->render();
			$loginView->footer = "";//View::factory("template-sections/footer-multiflex")->render();
		
			$styles = array(
					'media/css/mf4_layout2_setup.css' => 'screen, projection, print',
					'media/css/mf4_layout2_text.css'  => 'screen, projection, print',
					'media/css/redmond/jquery-ui-1.7.1.custom.css' => 'screen, projection',
					'media/css/redmond/ui.datepicker.css' => 'screen, projection',
					'media/js/fancybox/jquery.fancybox-1.3.4.css' => 'screen, projection',
					'media/css/diastole.css' => 'screen, projection',
					'media/css/custom_css.css'=>'screen,projection' //a removerse
			);
		  
			$loginView->styles = $styles;
			$loginView->scripts = array();
				
			$this->request->response = $loginView->render();			
		}
		
		public function action_loginform(){
			$this->request->response = View::factory("auth/loginform")->render();
		}
		
		public function action_registerform(){
			$this->request->response = View::factory("auth/registerform")->render();	
		}
				
		public function action_register(){
			if(Auth::instance()->logged_in()!= 0){	#If user already signed-in
				Request::instance()->redirect('welcome'); #redirect to the user account		
			}
 
			#Load the view	//$content = View::factory('register');		
 
			#If there is a post and $_POST is not empty
			if ($_POST){
				#Instantiate a new user
				$user = ORM::factory('user');	
 
				#Load the validation rules, filters etc...
				$post = $user->validate_create($_POST);			
 
				#If the post data validates using the rules setup in the user model
				if ($post->check()){
					$user->values($post); #Affects the sanitized vars to the user object
	 
					$user->save(); #create the account
	 
					#Add the login role to the user
					//$login_role = new Model_Role(array('name' =>'login'));//$user->add('roles',$login_role);
					$login_role = ORM::factory('role')->where('name', '=', 'login')->find();
					$user->add('roles',$login_role);
						 
					#sign the user in
					Auth::instance()->login($post['username'], $post['password']);
	 
					#redirect to the user account
					Request::instance()->redirect('welcome');
					
				}else{
	                #Get errors for display in view
					//$content->errors = $post->errors('register');
					$this->request->response = Fire::log($post->errors('register'));
					
				}			
			}		
				
		}
		
		public function action_signin(){
			
			#If user already signed-in
			if(Auth::instance()->logged_in()!= 0){
				#redirect to the user account
				Request::instance()->redirect('welcome');		
			}
 
			//$content = View::factory('signin');	
 
			#If there is a post and $_POST is not empty
			if ($_POST){
				#Instantiate a new user
				$user = ORM::factory('user');
	 
				#Check Auth
				$status = $user->login($_POST);
	 
				#If the post data validates using the rules setup in the user model
				if ($status){		
					#redirect to the user account
					Request::instance()->redirect('welcome');
				}else
				{
	                #Get errors for display in view
					//$content->errors = $_POST->errors('signin');
					$this->request->response = var_dump($_POST->errors('signin')); 
				}
			}
		}
		
		public function action_signout(){
			#Sign out the user
			Auth::instance()->logout(true);//con "true" destruye por completo la sesión
 
			#redirect to the user account and then the signin page if logout worked as expected
			Request::instance()->redirect('login');
		}
		
		
		public function action_createuser(){
			$model = ORM::factory('user');
			$model->values(array(
   				'username' => 'admin',
   				'email' => 'admin@example.com',
   				'password' => 'admin',
   				'password_confirm' => 'admin',
			));
			$model->save();
			// remember to add the login role AND the admin role
			// add a role; add() executes the query immediately
			$model->add('roles', ORM::factory('role')->where('name', '=', 'login')->find());
			$model->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find());
			$model->save();
				
		}
		
	}

?>